const User = require('../model/User');
var express = require('express');
// 数据验证模块，
const validator = require('validator');
const Dbset = require('../model/db');
const Games = require('../model/Games');
const Reply = require('../model/Reply');
const setting = require('../setting');
const message = require('../common/message');
var router = express.Router();
exports.allgames = (req, res, next) => {
    // console.log('asd123')
    console.log(req.query)
    var pages = req.query.pages;
    var pages = parseInt(pages);

    Games.find().skip(2 * pages).limit(2).exec(function (err, data) {
        // console.log(data)
        if (err) {
            res.json({ error: 1, message: '程序员祭天了' })
        }
        res.json({ error: 0, data: data })
    })
}

exports.sones = (req, res, next) => {
    var id = req.body.ids.name;
    Games.findOne({ "gameid": id }).exec(function (err, data) {
        if (err) {
            res.json({ error: 1, message: '游戏下架' })
        }
        res.json({ error: 0, data: data })
    })
}
exports.gameing = (req, res, next) => {
    var isLogoin = req.session.user
    // console.log("123" + isLogoin._id)
    if (isLogoin == null) {
        res.json({ error: 1, message: '请先登录' })
    } else {
        res.json({ error: 0, message: isLogoin })
    }
}

exports.buys = (req, res, next) => {
    var id = req.session.user._id
    User.findOne({ "_id": id }, function (err, user) {
        console.log(user.game_id)
        if (user.game_id.length <= 0) {
            user.game_id.push(req.body.gid)
            user.save()
            return res.json({ error: 0, message: "购买成功" })
        } else {
            var boos = false;
            user.game_id.forEach(function (data) {
                if (data == req.body.gid) {

                    return boos = true
                }
            });
            if (!boos) {
                user.game_id.push(req.body.gid)
                user.save()
            } else {
                return res.json({ error: 1, message: "您购买过了，请下载，谢谢您的支持" })
            }
        }
        res.json({ error: 0, message: "购买成功" })

    })
}
// =========回复=======
exports.reply = (req, res, next) => {
    // console.log(req.body.gid.name)
    // console.log(req.body.uid)
    // console.log(req.body.content)
    // console.log(req.body.toux)
    var content = validator.trim(String(req.body.content))
    if (content === '') {
        return res.json({ error: 0, message: '内容不能为空，评价失败' })
    }
    User.findOne({ "_id": req.body.uid }, (err, user) => {
        // console.log(user)
        let reply = new Reply()
        // console.log(reply)
        reply.content = content;
        reply.content_img = req.body.toux;
        reply.create_time = new Date;
        reply.author = req.body.uid;
        reply.games_id = req.body.gid.name;
        // reply.deleted = false;
        reply.save().then((replys) => {
            res.json({ error: 0, message: "回复成功" })
            return replys;
        }).then((replys) => {
            user.reply_count += 1
            user.save()
            return replys;
        }).catch((err) => {
            return res.end(err)
        })
    })
}
//=========获取所有回复===========
exports.getreply = (req, res, next) => {
    // console.log('12313212132132132132')
    // console.log(req.body.idd.name)
    Reply.find({ games_id: req.body.idd.name }).populate('author').then((replys) => {
        res.json({ error: 0, redata: replys })
    }).catch((err) => {
        res.end(err)
    })
}
exports.mylike = (req, res, next) => {
    console.log(req.query.lk)
    Reply.findById({ "_id": req.query.lk }, (err, data) => {
        console.log(data.likes)
        data.likes++
        data.save()
        res.json({ error: 0, likes: data.likes })
    })
}
exports.twore = (req, res, next) => {
    // console.log(req.body)
    // console.log(req.session.user)
    if (req.session.user == null) {
        return res.json({ error: 1, message: "请登录在回复好不好？" })
    }
    var twore = req.session.user
    User.findOne({ "_id": twore }, (err, user) => {
        // console.log(user)
        let reply = new Reply()
        // console.log(reply)
        reply.create_time = new Date;
        reply.author = req.body.uid;
        reply.games_id = req.body.oneid
        reply.reply_id = req.body.gid.name;
        // console.log(req.body.gid)
        // reply.bereply_id = twore
        // reply.deleted = false;
        reply.save().then((replys) => {
            res.json({ error: 0, message: "二级回复成功" })
            return replys;
        }).then((replys) => {
            user.reply_count += 1
            user.save()
        }).then((replys) => {
            Reply.findById({ "_id": req.body.oneid }, (err, data) => {
                // console.log(data)
                data.comment_num++
                data.save()
            })
            return replys;
        }).then((replys) => {
            if (reply.author.toString() !== req.session.user._id.toString()) {
                message.sendReplyMessage(reply.author, req.session.user._id, reply.games_id, reply.reply_id);
            }
            return replys;
        }).catch((err) => {
            return res.end(err)
        })
    })

}
exports.allteore = (req, res, next) => {
    console.log('------------------------------------------')
    var oneid = req.body.oneid
    // console.log(oneid)
    Reply.find({ games_id: oneid }).populate('author Reply').then((replys) => {
        res.json({ error: 0, data: replys })
    }).catch((err) => {
        console.log(err)
    })
}
